<?php

namespace App\Models;

use App\Util\Tool;
use App\Util\Url;
use Encore\Admin\Traits\AdminBuilder;
use Encore\Admin\Traits\ModelTree;
use Illuminate\Database\Eloquent\Model;
use Illuminate\Support\Facades\DB;

/**
 *课程分类
 */
class CourseCategory extends Model
{
    use ModelTree, AdminBuilder;

    protected $table = 'c_course_categories';

    protected $fillable = ['pid', 'classify_name', 'sort'];

    protected $with = [
        'parent'
    ];


    public function __construct(array $attributes = [])
    {
        parent::__construct($attributes);
        $this->setParentColumn('pid');  // 父ID
        $this->setOrderColumn('sort'); // 排序
        $this->setTitleColumn('classify_name'); // 标题
        $this->setParentColumn('pid');//删除使用的pid
    }

    /**
     * 从写方法用户前端数据禁止删除功能
     *
     * @return mixed
     */
    public function allNodes()
    {
        $orderColumn = DB::getQueryGrammar()->wrap($this->orderColumn);
        $byOrder = $orderColumn . ' = 0,' . $orderColumn;

        $self = new static();

        if ($this->queryCallback instanceof \Closure) {
            $self = call_user_func($this->queryCallback, $self);
        }
        $res = $self->orderByRaw($byOrder)->get()->toArray();
        $result = [];
        foreach ($res as $key => $val) {
            if (in_array($val['id'], [1, 2, 3, 4, 5, 6, 7])) {
                $val['is_show'] = 1;
            } else {
                $val['is_show'] = 0;
            }
            $result[$key] = $val;
        }
        return $result;
    }

    /**
     * 该分类的子分类
     */
    public function child()
    {
        return $this->hasMany(get_class($this), 'pid', $this->getKeyName());
    }

    /**
     * 该分类的父分类
     */
    public function parent()
    {
        return $this->hasOne(get_class($this), $this->getKeyName(), 'pid');
    }

    /**
     * @return array
     * 获取课程分类列表
     */
    public function getCourseCategoryList($param)
    {
        $where = [];
        $limit = 15;
        //课程首页分类图标
        if (isset($param['type']) && $param['type'] == 1) {
            $w[] = [function ($query) {
                return $query->whereIn('id', [1, 2, 3, 4, 5, 6, 7]);
            }];
            $res = $this::where($w)->skip(0)->take($limit)->get()->toArray();
            foreach ($res as $k => &$v) {
                if ($v['classify_picture']) {
                    $v['classify_picture'] = Url::getHttpHost() . 'upload/' . $v['classify_picture'];
                }
            }
            return $res;
        }

        if (isset($param['pid'])) {
            $where[] = ['pid', '=', $param['pid']];
        }
        if (!empty($param['limit'])) {
            $limit = $param['limit'];
        }
        $res = $this::orderBy("sort")->where($where)->skip(0)->take($limit)->get()->toArray();
        foreach ($res as $k => &$v) {
            if ($v['classify_picture']) {
                $v['classify_picture'] = Url::getHttpHost() . 'upload/' . $v['classify_picture'];
            }
        }
        $tree = (new Tool())::buildTree($res);
        return $tree;
    }

    /**获取分类名称
     * @param $id
     * @return mixed
     */
    public function getName($id)
    {
        $res = $this->select('classify_name')->find($id);
        if (!empty($res)) {
            return $res->classify_name;
        } else {
            return '';
        }
    }

    /**
     * 获取下拉列表
     * @return array
     */
    public function getOptions()
    {
        $options = $this->select('id', 'classify_name as text')->get();
        $selectOption = [];
        foreach ($options as $option) {
            $selectOption[$option->id] = $option->text;
        }
        return $selectOption;
    }
}
